<?php
class System extends Dao
{
	private static $_instance = null;
	protected $_db = null, $_cache = null;
	private $_phpInfo, $_systemInfo, $_stats;
	
	protected function __construct()
	{
		parent::__construct();
	}
	
	public static function getInstance() 
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
	
	/**
	 * Get PHP Info
	 *
	 * @return array some common php settings
	 *
	 * @author: AnhDKD
	 */
	public function &getPhpInfo()
	{
		if (is_null($this->_phpInfo))
		{
			$this->_phpInfo = array();
			$this->_phpInfo['display_errors']		= ini_get('display_errors') == '1';
			$this->_phpInfo['short_open_tag']		= ini_get('short_open_tag') == '1';
			$this->_phpInfo['magic_quotes_gpc']		= ini_get('magic_quotes_gpc') == '1';
			$this->_phpInfo['file_uploads']			= ini_get('file_uploads') == '1';
			$this->_phpInfo['post_max_size']		= ini_get('post_max_size');
			$this->_phpInfo['upload_max_filesize']	= ini_get('upload_max_filesize');
			$this->_phpInfo['max_execution_time']	= ini_get('max_execution_time');
			
			
			
			$this->_phpInfo['register_globals']		= ini_get('register_globals') == '1';
			$this->_phpInfo['output_buffering']		= (bool) ini_get('output_buffering');
			$this->_phpInfo['open_basedir']			= ini_get('open_basedir');
			$this->_phpInfo['session.save_path']	= ini_get('session.save_path');
			$this->_phpInfo['session.auto_start']	= ini_get('session.auto_start');
			$this->_phpInfo['disable_functions']	= ini_get('disable_functions');
			$this->_phpInfo['xml']					= extension_loaded('xml');
			$this->_phpInfo['zlib']					= extension_loaded('zlib');
			$this->_phpInfo['zip']					= function_exists('zip_open') && function_exists('zip_read');
			$this->_phpInfo['mbstring']				= extension_loaded('mbstring');
			$this->_phpInfo['iconv']				= function_exists('iconv');
		}
		return $this->_phpInfo;
	}
	
	/**
	 * Method to get the system information
	 *
	 * @return  array system information values
	 *
	 * @since   1.6
	 */
	public function &getInfo()
	{
		if (is_null($this->_systemInfo))
		{
			$this->_systemInfo = array();
			$pdo = $this->_db->pdoInstance;

			//get db collation
			$db_collation = $this->_db->charset;
			if($pdo->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql'){
				$row = $this->_db->createCommand('SHOW VARIABLES LIKE "character_set_database";')->queryRow();
				$db_collation = $row['value'];
			}

			$this->_systemInfo['os'] 				= php_uname(); //information about the operating system PHP is running on
			$this->_systemInfo['server_ip']			= $_SERVER['SERVER_ADDR'];
			$this->_systemInfo['server_name']			= isset($_SERVER['SERVER_SOFTWARE'])?$_SERVER['SERVER_SOFTWARE']:getenv('SERVER_SOFTWARE');
			$this->_systemInfo['php_version']		= phpversion();
			$this->_systemInfo['db_driver_name']			= $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
			$this->_systemInfo['db_version']		= $pdo->getAttribute(PDO::ATTR_SERVER_VERSION);
			$this->_systemInfo['db_collation']		= $db_collation;
			$this->_systemInfo['sapi_name']			= php_sapi_name(); //the type of interface between web server and PHP
		}
		return $this->_systemInfo;
	}
	
	public function getStats()
	{
		$arrStats = array();
		
		//taxonomy
		$strSql = "SELECT taxonomy_name, COUNT(*) FROM tbl_cms_posts p INNER JOIN tbl_cms_taxonomies t ON p.taxonomy_id = t.taxonomy_id GROUP BY p.taxonomy_id;";
		$arrStats['taxonomy'] = $this->_db->createCommand($strSql)->queryScalar();
		
		//posts
		$strSql = "SELECT * FROM tbl_cms_posts WHERE post_parent is null ORDER BY post_created_date DESC LIMIT 0, 5;";
		$arrStats['post'] = $this->_db->createCommand($strSql)->queryAll();
		
		return $this->_stats = array(
			'taxonomy' => $arrStats['taxonomy'],
			'post' => $arrStats['post']
		);
	}
}